#define _CRT_SECURE_NO_WARNINGS
#include "BinaryTree.h"

BTNode* BuyNewNode(BTDataType x)
{
	BTNode* NewNode = (BTNode*)malloc(sizeof(BTNode));
	if (NewNode == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	NewNode->data = x;
	NewNode->left = NULL;
	NewNode->right = NULL;
	return NewNode;
}


int BTHeight(BTNode* root)
{
	if (root == NULL)
		return 0;
	int left = BTHeight(root->left);
	int right = BTHeight(root->right);

	return left > right ? left + 1 : right + 1;
}

int BTSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	int left = BTSize(root->left);
	int right = BTSize(root->right);
	return left + right + 1;
}


int TreeKLevel(BTNode* root,int k)
{
	if (root == NULL)
		return 0;
	if (k == 1)
		return 1;
	int lnum = TreeKLevel(root->left, k - 1);
	int rnum = TreeKLevel(root->right, k - 1);
	return lnum + rnum;
}